package com.zjsru.plan2024.oneday;

/**
 * @Author: cookLee
 * @Date: 2024-01-14
 * 删除排序链表中的重复元素
 */
public class DeleteDuplicates {

    /**
     * 主
     * \
     * 输入：head = [1,1,2]
     * 输出：[1,2]
     * \
     * 输入：head = [1,1,2,3,3]
     * 输出：[1,2,3]
     * \
     * @param args args
     */
    public static void main(String[] args) {
        ListNode head = new ListNode(1,new ListNode(1,new ListNode(2)));
        DeleteDuplicates deleteDuplicates = new DeleteDuplicates();
        System.out.println(deleteDuplicates.deleteDuplicates(head));
    }

    /**
     * 删除重复项
     *
     * @param head 封头
     * @return {@link ListNode}
     */
    public ListNode deleteDuplicates(ListNode head) {
        ListNode curr = head;
        while (curr != null && curr.next != null) {
            if (curr.val == curr.next.val) {
                curr.next = curr.next.next;
            } else {
                curr = curr.next;
            }
        }
        return head;
    }

}
